# coding = utf-8


''''
企业数据的验证，同时包括肝脏和肿瘤
'''

import sys

import click
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from pathlib2 import Path
from tensorboardX import SummaryWriter
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from tqdm import tqdm

import utils.checkpoint as cp
from dataset import KiTS19
from dataset.transform import MedicalTransform
from loss import GeneralizedDiceLoss
from loss.util import class2one_hot
from network.m_net import UNet
from utils.metrics import Evaluator
from utils.vis import imshow
import matplotlib.pyplot as plt
import os
import cv2


def evaluation(net, dataset, batch_size, num_workers, type):
    type = type.lower()
    if type == 'train':
        subset = dataset.train_dataset
        case_slice_indices = dataset.train_case_slice_indices
    elif type == 'valid':
        subset = dataset.valid_dataset
        case_slice_indices = dataset.valid_case_slice_indices

    sampler = SequentialSampler(subset)
    data_loader = DataLoader(subset, batch_size=batch_size, sampler=sampler,
                             num_workers=num_workers, pin_memory=True)
    evaluator = Evaluator(dataset.num_classes)

    case = 0
    vol_label = []
    vol_output = []
    vol_images = []
    result_map = {}

    with tqdm(total=len(case_slice_indices) - 1, ascii=True, desc=f'eval/{type:5}', dynamic_ncols=True) as pbar:
        for batch_idx, data in enumerate(data_loader):
            imgs, labels, idx = data['image'].cuda(), data['label'], data['index']

            outputs = net(imgs)
            outputs = outputs.argmax(dim=1)

            labels = labels.cpu().detach().numpy()
            outputs = outputs.cpu().detach().numpy()
            imgs = imgs.cpu().detach().numpy()
            idx = idx.numpy()

            vol_label.append(labels)
            vol_output.append(outputs)
            vol_images.append(imgs)

            while case < len(case_slice_indices) - 1 and idx[-1] >= case_slice_indices[case + 1] - 1:
                vol_output = np.concatenate(vol_output, axis=0)
                vol_label = np.concatenate(vol_label, axis=0)
                vol_images = np.concatenate(vol_images, axis=0)

                vol_num_slice = case_slice_indices[case + 1] - case_slice_indices[case]
                evaluator.add(vol_output[:vol_num_slice], vol_label[:vol_num_slice])

                output_case = vol_output[:vol_num_slice]
                label_case = vol_label[:vol_num_slice]
                images_case = vol_images[:vol_num_slice]

                #print(output_case.shape, label_case.shape, images_case.shape)

                '''
                origion_path = Path("/home/diaozhaoshuo/log/BeliefFunctionNN/chengkung/dongbeidaxue/munet/image_tumor_weight")
                case_path = origion_path / "case_{}".format(str(case + 35).zfill(5))
                label_path = case_path / "label"
                predict_path = case_path / "predict"
                fusion_path = case_path / "fusion"
                predict_fusion_path = case_path / "fusion_predict"
                #image_path = case_path/ "image"

                if not label_path.exists():
                    label_path.mkdir(parents=True)
                if not predict_path.exists():
                    predict_path.mkdir(parents=True)
                if not fusion_path.exists():
                    fusion_path.mkdir(parents=True)
                if not predict_fusion_path.exists():
                    predict_fusion_path.mkdir(parents=True)

                for i in range(output_case.shape[0]):
                    if np.max(output_case[i]) == 0:
                        continue
                    plt.imsave(os.path.join(str(predict_path), "{}.png".format(str(i).zfill(3))), output_case[i],
                               cmap="gray")

                for i in range(label_case.shape[0]):
                    if np.max(label_case[i]) == 0:
                        continue
                    plt.imsave(os.path.join(str(label_path), "{}.png".format(str(i).zfill(3))), label_case[i],
                               cmap="gray")

                for i in range(images_case.shape[0]):
                    image = images_case[i][1] * 255
                    image = image.astype(np.uint8)
                    image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
                    label = label_case[i]

                    if np.max(label) == 0:
                        continue

                    #plt.imsave(os.path.join(str(image_path), "{}.png".format(str(i).zfill(3))), image)

                    if 1 in np.unique(label):
                       label_copy = np.zeros(label.shape)
                       label_copy[label==1] = 1
                       label_copy[label==2] = 1
                       label_copy = label_copy*255
                       label_copy = label_copy.astype(np.uint8)
                       contours, _ = cv2.findContours(label_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                       for counter in contours:
                          data_list = []
                          for t in range(counter.shape[0]):
                            j = counter[t][0]
                            data_list.append(j)
                          cv2.polylines(image, np.array([data_list], np.int32), True, [0, 255, 0], thickness=1)

                    if 2 in np.unique(label):
                       label_copy = np.zeros(label.shape)
                       label_copy[label == 2] = 1
                       label_copy = label_copy * 255
                       label_copy = label_copy.astype(np.uint8)
                       contours, _ = cv2.findContours(label_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                       for counter in contours:
                         data_list = []
                         for t in range(counter.shape[0]):
                            j = counter[t][0]
                            data_list.append(j)
                         cv2.polylines(image, np.array([data_list], np.int32), True, [255, 0, 0], thickness=1)

                    plt.imsave(os.path.join(str(fusion_path), "{}.png".format(str(i).zfill(3))), image)

                for i in range(images_case.shape[0]):
                    image = images_case[i][1] * 255
                    image = image.astype(np.uint8)
                    image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
                    label = output_case[i]

                    if np.max(label) == 0:
                        continue

                    # plt.imsave(os.path.join(str(image_path), "{}.png".format(str(i).zfill(3))), image)

                    if 1 in np.unique(label):
                        label_copy = np.zeros(label.shape)
                        label_copy[label == 1] = 1
                        label_copy[label == 2] = 1
                        label_copy = label_copy * 255
                        label_copy = label_copy.astype(np.uint8)
                        contours, _ = cv2.findContours(label_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                        for counter in contours:
                            data_list = []
                            for t in range(counter.shape[0]):
                                j = counter[t][0]
                                data_list.append(j)
                            cv2.polylines(image, np.array([data_list], np.int32), True, [0, 255, 0], thickness=1)

                    if 2 in np.unique(label):
                        label_copy = np.zeros(label.shape)
                        label_copy[label == 2] = 1
                        label_copy = label_copy * 255
                        label_copy = label_copy.astype(np.uint8)
                        contours, _ = cv2.findContours(label_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                        for counter in contours:
                            data_list = []
                            for t in range(counter.shape[0]):
                                j = counter[t][0]
                                data_list.append(j)
                            cv2.polylines(image, np.array([data_list], np.int32), True, [255, 0, 0], thickness=1)

                    plt.imsave(os.path.join(str(predict_fusion_path), "{}.png".format(str(i).zfill(3))), image)
                '''

                ####统计一些per_case的指标

                output_case1 = np.zeros(output_case.shape)
                label_case1 = np.zeros(label_case.shape)
                output_case1[output_case == 1] = 1
                output_case1[output_case == 2] = 1
                label_case1[label_case == 1] = 1
                label_case1[label_case == 2] = 1
                dice1 = 2 * (output_case1*label_case1).sum() / (output_case1.sum() + label_case1.sum())
                precision1 = (output_case1[label_case1 == 1] == 1).sum() / (output_case1 == 1).sum()
                recall1 = (output_case1[label_case1 == 1] == 1).sum() / (label_case1 == 1).sum()

                output_case2 = np.zeros(output_case.shape)
                label_case2 = np.zeros(label_case.shape)
                output_case2[output_case == 2] = 1
                label_case2[label_case == 2] = 1
                dice2 = 2 * (output_case2 * label_case2).sum() / (output_case2.sum() + label_case2.sum())
                precision2 = (output_case2[label_case2 == 1] == 1).sum() / (output_case2 == 1).sum()
                recall2 = (output_case2[label_case2 == 1] == 1).sum() / (label_case2 == 1).sum()

                result_map[label_case2.sum()] = [case+35,dice1, dice2, precision1, recall1,
                                                 precision2, recall2]






                vol_output = [vol_output[vol_num_slice:]]
                vol_label = [vol_label[vol_num_slice:]]
                vol_images = [vol_images[vol_num_slice:]]

                case += 1
                pbar.update(1)

    acc = evaluator.eval()


    for k in sorted(list(acc.keys())):
        if k == 'dc_each_case': continue
        print(f'{type}/{k}: {acc[k]:.5f}')


    for case_idx in range(len(acc['dc_each_case'])):
        case_id = dataset.case_idx_to_case_id(case_idx, type)
        dc_each_case = acc['dc_each_case'][case_idx]
        for cls in range(len(dc_each_case)):
            dc = dc_each_case[cls]


    tumor_dice_100w = []
    liver_dice_100w = []
    tumor_dice_10w = []
    liver_dice_10w = []
    tumor_dice_1w = []
    liver_dice_1w = []
    tumor_dice_small = []
    liver_dice_small = []


    for key in sorted(result_map.keys(), reverse=True):
        print(key, result_map[key])
        tumor_dice = result_map[key][2]
        liver_dice = result_map[key][1]
        if key >= 1000000:
            tumor_dice_100w.append(tumor_dice)
            liver_dice_100w.append(liver_dice)
        elif key >= 100000:
            tumor_dice_10w.append(tumor_dice)
            liver_dice_10w.append(liver_dice)
        elif key >= 10000:
            tumor_dice_1w.append(tumor_dice)
            liver_dice_1w.append(liver_dice)
        else:
            tumor_dice_small.append(tumor_dice)
            liver_dice_small.append(liver_dice)

    print(np.mean(np.array(tumor_dice_100w)), np.mean(np.array(liver_dice_100w)),
          np.mean(np.array(tumor_dice_10w)), np.mean(np.array(liver_dice_10w)),
          np.mean(np.array(tumor_dice_1w)), np.mean(np.array(liver_dice_1w)),
          np.mean(np.array(tumor_dice_small)), np.mean(np.array(liver_dice_small)))


    # score = (acc['dc_per_case_1'] + acc["dc_per_case_2"]) / 2
    # return score


def validate_evaluation():
    dataset = KiTS19("/datasets/DongbeiDaxue/chengkunv2", stack_num=3, spec_classes=[0, 1, 2], img_size=(512, 512),
                     use_roi=False, roi_file=None, roi_error_range=5,
                     train_transform=None, valid_transform=None)

    model_01 = UNet(input_channel=dataset.img_channels, n_class=dataset.num_classes)
    data = {'net': model_01}



    cp_file = Path("/home/diaozhaoshuo/log/BeliefFunctionNN/chengkung/dongbeidaxue/munet/checkpoint_liver_and_tumor/best.pth")
    cp.load_params(data, cp_file, device='cpu')
    model_01 = model_01.cuda()
    model_01.eval()
    evaluation(model_01, dataset, batch_size=3, num_workers=1, type="valid")
    #evaluation(model_01, dataset, batch_size=1, num_workers=1, type="train")


if __name__ == '__main__':
    validate_evaluation()